package com.tsx.test.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsx.test.entity.dto.DeptNodeDTO;
import com.tsx.test.entity.po.Department;
import com.tsx.test.entity.vo.DeptNode;
import com.tsx.test.mapper.DepartmentMapper;
import com.tsx.test.service.DepartmentService;
import com.tsx.test.utils.TreeUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sheng
 * @description 针对表【department】的数据库操作Service实现
 * @createDate 2025-07-01 16:35:52
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department>
        implements DepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public List<DeptNode> treeList() {
        List<Department> departments = departmentMapper.selectList(null);
        List<DeptNode> deptNodeList = departments.stream()
                .map(department -> new DeptNode(department.getId(), department.getName(), department.getLeader(), department.getParentId(), new ArrayList<>()))
                .collect(Collectors.toList());
        return TreeUtil.buildTree2(deptNodeList, 0L);
    }

    @Override
    public List<Department> importDept(List<DeptNodeDTO> deptNodeList) {
        return TreeUtil.flattenTree(deptNodeList, 0L);
    }
}




